/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_LnxLibThin.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2014/11/07 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

#include "frcomm.h"
#include "frstr.h"
#include "frmem.h"
#include "frlog.h"

#include "../../ora_core.h"
#include "../../comm/ora_comm.h"
#include "../../comm/ora_util.h"
#include "ora_NUMBER.h"

FR_STATIC char lnx_chars[50] = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', ' ', '.', ',', '$', '<', '>', '(', ')', '#', '~', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i',
        'l', 'm', 'p', 'r', 's', 't', 'v', 'A', 'B', 'C', 'D', 'E', 'F', 'I', 'L', 'M', 'P', 'R', 'S', 'T'
};

FR_STATIC int LnxqRound_P_Len = 101;
FR_STATIC char LnxqRound_P[101] = {0, 1, 1, 1, 1, 1, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 21, 21, 21, 21, 21, 21, 21, 21, 21,
        21, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 51, 51,
        51, 51, 51, 51, 51, 51, 51, 51, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 71, 71, 71, 71, 71,
        71, 71, 71, 71, 71, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 91, 91, 91, 91, 91, 91, 91, 91,
        91, 91, 101, 101, 101, 101, 101};

FR_STATIC int LnxqRound_N_Len = 102;
FR_STATIC char LnxqRound_N[102] = {0, 0, 1, 1, 1, 1, 1, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 21, 21, 21, 21, 21, 21, 21, 21,
        21, 21, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 51,
        51, 51, 51, 51, 51, 51, 51, 51, 51, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 71, 71, 71, 71,
        71, 71, 71, 71, 71, 71, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 91, 91, 91, 91, 91, 91, 91,
        91, 91, 91, 101, 101, 101, 101, 101};

FR_STATIC int LnxqComponents_P_Len = 202;
FR_STATIC char LnxqComponents_P[][2] = {
        {0, 0}, {0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, {0, 8}, {0, 9}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, {2, 0}, {2, 1},
        {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, {3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, {4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4},
        {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4, 9}, {5, 0}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {5, 7}, {5, 8}, {5, 9}, {6, 0}, {6, 1}, {6, 2}, {6, 3}, {6, 4}, {6, 5}, {6, 6}, {6, 7},
        {6, 8}, {6, 9}, {7, 0}, {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 5}, {7, 6}, {7, 7}, {7, 8}, {7, 9}, {8, 0}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 6}, {8, 7}, {8, 8}, {8, 9}, {9, 0},
        {9, 1}, {9, 2}, {9, 3}, {9, 4}, {9, 5}, {9, 6}, {9, 7}, {9, 8}, {9, 9}
};

FR_STATIC int LnxqComponents_N_Len = 204;
FR_STATIC char LnxqComponents_N[][2] = {
        {0, 0}, {0, 0}, {9, 9}, {9, 8}, {9, 7}, {9, 6}, {9, 5}, {9, 4}, {9, 3}, {9, 2}, {9, 1}, {9, 0}, {8, 9}, {8, 8}, {8, 7}, {8, 6}, {8, 5}, {8, 4}, {8, 3}, {8, 2}, {8, 1}, {8, 0}, {7, 9},
        {7, 8}, {7, 7}, {7, 6}, {7, 5}, {7, 4}, {7, 3}, {7, 2}, {7, 1}, {7, 0}, {6, 9}, {6, 8}, {6, 7}, {6, 6}, {6, 5}, {6, 4}, {6, 3}, {6, 2}, {6, 1}, {6, 0}, {5, 9}, {5, 8}, {5, 7}, {5, 6},
        {5, 5}, {5, 4}, {5, 3}, {5, 2}, {5, 1}, {5, 0}, {4, 9}, {4, 8}, {4, 7}, {4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}, {3, 9}, {3, 8}, {3, 7}, {3, 6}, {3, 5}, {3, 4}, {3, 3},
        {3, 2}, {3, 1}, {3, 0}, {2, 9}, {2, 8}, {2, 7}, {2, 6}, {2, 5}, {2, 4}, {2, 3}, {2, 2}, {2, 1}, {2, 0}, {1, 9}, {1, 8}, {1, 7}, {1, 6}, {1, 5}, {1, 4}, {1, 3}, {1, 2}, {1, 1}, {1, 0},
        {0, 9}, {0, 8}, {0, 7}, {0, 6}, {0, 5}, {0, 4}, {0, 3}, {0, 2}, {0, 1}, {0, 0}
};

FR_STATIC int LnxqFirstDigit_Len = 102;
FR_STATIC char LnxqFirstDigit[102] = {
        0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};

/*******************************************************************************
*  FUNC     :  setLength
*  ARGS     :  *p_data     (数据内容)
*           :  ui_len      (数据长度)
*           :  i_param
*           :  **pp_out    (解析完成的数据)
*  RTN      :  0       (FR_SUCC)
*              1       (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_setLength(char *p_data, unsigned int ui_len, int i_param, char **pp_out, int *p_i_out_len)
{
    int i_is_positive = 0;
    char *p_out = NULL;

    i_is_positive = ora_NUMBER_isPositive(p_data);
    if (i_is_positive == FR_SUCC)
    {
        if ((p_out = (char *) frmalloc(i_param * sizeof(char))) == NULL)
        {
            ORA_MALLOCK_ERR;
            goto error;
        }
    }
    else if (i_param <= 20 && (p_data[(i_param - 1)] != 102))
    {
        if ((p_out = (char *) frmalloc((i_param + 1) * sizeof(char))) == NULL)
        {
            ORA_MALLOCK_ERR;
            goto error;
        }
        p_out[i_param] = 102;
    }
    else
    {
        if ((p_out = (char *) frmalloc(i_param * sizeof(char))) == NULL)
        {
            ORA_MALLOCK_ERR;
            goto error;
        }
    }

    memcpy(p_out, p_data, i_param);
    *pp_out = p_out;
    *p_i_out_len = i_param;
    return FR_SUCC;
error:
    ora_free((void **) &p_out);
    return FR_FAIL;
}

/*******************************************************************************
*  FUNC     :  lnxfpr
*  ARGS     :  *p_data     (数据内容)
*           :  ui_len      (数据长度)
*           :  i_param
*           :  **pp_out    (解析完成的数据)
*  RTN      :  0       (FR_SUCC)
*              1       (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_lnxfpr(char *p_data, unsigned int ui_len, int i_param, char **pp_out, int *p_i_out_len)
{
    int k = ui_len;
    if (ora_NUMBER_isZero(p_data, ui_len) == FR_SUCC)
        return ora_NUMBER_makeZero(pp_out, p_i_out_len);
    if (ora_NUMBER_isNegInf(p_data, ui_len) == FR_SUCC)
        return ora_NUMBER_makeNegInf(pp_out, p_i_out_len);
    if (ora_NUMBER_isPosInf(p_data, ui_len) == FR_SUCC)
        return ora_NUMBER_makePosInf(pp_out, p_i_out_len);
    if (i_param < 0)
        return ora_NUMBER_makeZero(pp_out, p_i_out_len);

    int i_is_positive = 0;
    int j = 0;
    int flag1 = 0;
    char byte2;
    char byte3;
    char byte4;
    i_is_positive = ora_NUMBER_isPositive(p_data);
    if (i_is_positive == FR_SUCC)
    {
        i_param += (p_data[1] & 0xFF) >= 11 ? 1 : 2;
        j = i_param >> 1;
        flag1 = (i_param & 1) == 1 ? 1 : 0;
        byte2 = 1;
        byte3 = 100;
        byte4 = 1;
    }
    else
    {
        i_param += (p_data[1] & 0xFF) <= 91 ? 1 : 2;
        j = i_param >> 1;
        flag1 = (i_param & 1) == 1 ? 1 : 0;
        byte2 = 101;
        byte3 = 2;
        byte4 = -1;
        k -= (p_data[k - 1] & 0xFF) != 102 ? 0 : 1;
    }

    char *p_out = NULL;
    if ((p_out = (char *) frmalloc(k * sizeof(char))) == NULL)
    {
        ORA_MALLOCK_ERR;
        return FR_FAIL;
    }
    memcpy(p_out, p_data, k);

    if (p_data[j] < 0 || p_data[j] >= LnxqFirstDigit_Len)
    {
        ora_free((void **) &p_out);
        return FR_FAIL;
    }
    if ((j > k -1) || ((j == k - 1) && (flag1 || LnxqFirstDigit[(unsigned char) p_data[j]] == 1)))
    {
        ora_free((void **) &p_out);
        return ora_setLength(p_data, ui_len, k, pp_out, p_i_out_len);
    }
    if (((j == 0) && (!flag1 || (i_is_positive ? p_data[1] < 51 : p_data[1] > 51))) || (j == 1 && (!flag1) && (i_is_positive ? p_data[1] < 6 : p_data[1] > 96)))
    {
        ora_free((void **) &p_out);
        return ora_NUMBER_makeZero(pp_out, p_i_out_len);
    }
    if (j == 0)
    {
        if (ora_NUMBER_isInf(p_data, ui_len) == FR_SUCC)
        {
            if (i_is_positive)
            {
                ora_free((void **) &p_out);
                return ora_NUMBER_makePosInf(pp_out, p_i_out_len);
            }
            else
            {
                ora_free((void **) &p_out);
                return ora_NUMBER_makeNegInf(pp_out, p_i_out_len);
            }
        }
        else
        {
            p_out[0] = (char) (p_data[0] + byte4);
            p_out[1] = (char) (byte2 + byte4);
            if (ora_setLength(p_out, ui_len, 2, pp_out, p_i_out_len) != FR_SUCC)
            {
                ora_free((void **) &p_out);
                return FR_FAIL;
            }
        }
    }

    char byte0;
    char byte1;
    byte0 = (char) j;
    byte1 = (char) j;
    if (flag1)
    {
        p_out[(unsigned char) byte0] = 0;
        if (i_is_positive ? p_data[byte1 + 1] > 50 : p_data[byte1 + 1] < 52)
            p_out[(unsigned char) byte0] = (char) (p_data[(unsigned char) byte1] + byte4);
        else
            p_out[(unsigned char) byte0] = p_data[(unsigned char) byte1];
    }
    else
    {
        if (i_is_positive)
        {
            if (p_data[(unsigned char) byte1] < 0 || p_data[(unsigned char) byte1] >= LnxqRound_P_Len)
            {
                ora_free((void **) &p_out);
                return FR_FAIL;
            }
            p_out[(unsigned char) byte0] = LnxqRound_P[(unsigned char) p_data[(unsigned char) byte1]];
        }
        else
        {
            if (p_data[(unsigned char) byte1] < 0 || p_data[(unsigned char) byte1] >= LnxqRound_N_Len)
            {
                ora_free((void **) &p_out);
                return FR_FAIL;
            }
            p_out[(unsigned char) byte0] = LnxqRound_N[(unsigned char) p_data[(unsigned char) byte1]];
        }
    }
    byte1--;
    int l = 0;
    if (p_out[(unsigned char) byte0] == byte3 + byte4)
    {
        for (; byte1 > 0 && p_data[(unsigned char) byte1] == byte3; byte1--);
        if (byte1 == 0)
        {
            if (ora_NUMBER_isInf(p_data, ui_len) == FR_SUCC)
            {
                if (i_is_positive)
                {
                    ora_free((void **) &p_out);
                    return ora_NUMBER_makePosInf(pp_out, p_i_out_len);
                }
                else
                {
                    ora_free((void **) &p_out);
                    return ora_NUMBER_makeNegInf(pp_out, p_i_out_len);
                }
            }
            else
            {
                p_out[0] = (char) (p_data[0] + byte4);
                p_out[1] = (char) (byte2 + byte4);
                if (ora_setLength(p_out, ui_len, 2, pp_out, p_i_out_len) != FR_SUCC)
                {
                    ora_free((void **) &p_out);
                    return FR_FAIL;
                }
            }
        }
        p_out[(unsigned char) byte1] = (char) (p_data[(unsigned char) byte1] + byte4);
        l = byte1 + 1;
        byte1--;
    }
    else if (p_data[(unsigned char) byte0] == byte2)
    {
        for (; p_data[(unsigned char) byte1] == byte2; byte1--);
        l = byte1 + 1;
    }
    else
        l = j + 1;

    if (ora_setLength(p_out, ui_len, l, pp_out, p_i_out_len) != FR_SUCC)
    {
        ora_free((void **) &p_out);
        return FR_FAIL;
    }
    ora_free((void **) &p_out);
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  lnxnuc
*  ARGS     :  *p_data     (数据内容)
*           :  ui_len      (数据长度)
*           :  i_param
*           :  **pp_out    (解析完成的数据)
*  RTN      :  0       (FR_SUCC)
*              1       (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_lnxnuc(char *p_data, unsigned int ui_len, int i_param, char **pp_out)
{
    char *p_data_dup = NULL;
    char arr_tmp[22] = {'\0'};
    int i12 = 0;
    int i5 = 46;
    int k = 0;
    char *arr_char1 = lnx_chars;
    char *p_char2 = NULL;
    int i_is_positive = 0;
    char *p_lnxqComponents = NULL;
    int i_lnxqComponents_len = 0;
    int i6 = 0;
    int i18 = 1;
    int i_check_loop = 0;
    int i_data_dup_len = ui_len;

    if (i_param == 0)
        goto error;

    p_data_dup = (char *) frmemdup(p_data, ui_len);

    if ((k = i_param < 0 ? 0 : 1) == 0)
        i_param = -i_param;

    if ((p_char2 = (char *) frmalloc(i_param * sizeof(char))) == NULL)
    {
        ORA_MALLOCK_ERR;
        goto error;
    }
    i_is_positive = ora_NUMBER_isPositive(p_data_dup);
    if (i_is_positive != FR_SUCC)
    {
        p_lnxqComponents = (char *) LnxqComponents_N;
        i6 = i_param - 1;
        i_lnxqComponents_len = LnxqComponents_N_Len;
    }
    else
    {
        p_lnxqComponents = (char *) LnxqComponents_P;
        i6 = i_param;
        i_lnxqComponents_len = LnxqComponents_P_Len;
    }

    while (i18 != 0)
    {
        i_check_loop++;
        if (i_check_loop > 300)
        {
#if APM_ORA_DBG
            char arr_hex[8192] = {0x00};
            ora_bin_to_hex_string(p_data, ui_len, arr_hex, sizeof(arr_hex));
            ORA_PRINT_EMERG("ora_lnxnuc dead loop:[%s].\n", arr_hex);
            FRLOG_ERROR_DETAIL("ora_lnxnuc dead loop:[%s].\n", arr_hex);
#endif
            goto error;
        }
        int m = k;
        int i19 = ui_len;
        int i4 = 0;
        int i17 = 0;
        if (i19 == 1)
        {
            if ((p_data_dup[0] & 0xFF) == 128)
            {
                if (m != 0)
                {
                    i4 = i_param - 1;
                    p_char2[i4] = arr_char1[0];
                }
                else
                {
                    if (i_param < 5)
                    {
                        goto error;
                    }
                    i4 = i_param - 5;
                    p_char2[i4] = arr_char1[0];
                    p_char2[(i4 + 1)] = arr_char1[41];
                    p_char2[(i4 + 2)] = arr_char1[10];
                    p_char2[(i4 + 3)] = arr_char1[0];
                    p_char2[(i4 + 4)] = arr_char1[0];
                }

                if (i4 != 0)
                {
                    for (i17 = 0; i17 < i4; i17++)
                    {
                        p_char2[i17] = arr_char1[12];
                    }
                }
                *pp_out = p_char2;
                goto success;
            }

            if (i_param < 2)
                goto error;
            i4 = i_param - 2;
            p_char2[i4] = arr_char1[11];
            p_char2[(i4 + 1)] = arr_char1[21];

            if (i4 != 0)
            {
                for (i17 = 0; i17 < i4; i17++)
                {
                    p_char2[i17] = arr_char1[12];
                }
            }
            *pp_out = p_char2;
            goto success;
        } // i19 == 1

        if (i19 == 2 && ((p_data_dup[0] & 0xFF) == 255) && ((p_data_dup[1] & 0xFF) == 101))
        {
            i4 = i_param - 1;
            p_char2[i4] = arr_char1[21];
            if (i4 != 0)
            {
                for (i17 = 0; i17 < i4; i17++)
                {
                    p_char2[i17] = arr_char1[12];
                }
            }
            *pp_out = p_char2;
            goto success;
        }

        int i2 = 0;
        int i3 = 0;
        int i7 = 0;
        if (i_is_positive)
        {
            if (i19 - 1 >= i_data_dup_len)
                goto error;
            if (p_data_dup[(i19 - 1)] == 102)
                i19--;
            i2 = i19 - 1;
            i7 = 2 * (62 - (p_data_dup[0] & 0xFF)) + ((p_data_dup[1] & 0xFF) < 92 ? 1 : 0);
            if (p_data_dup[i2] < 0 || p_data_dup[i2] >= LnxqFirstDigit_Len)
                goto error;
            i3 = 2 * i2 - ((p_data_dup[1] & 0xFF) > 91 ? 1 : 0) - LnxqFirstDigit[(unsigned char) p_data_dup[i2]];
        }
        else
        {
            i2 = i19 - 1;
            i7 = 2 * ((p_data_dup[0] & 0xFF) - 193) + ((p_data_dup[1] & 0xFF) > 10 ? 1 : 0);
            if (p_data_dup[i2] < 0 || p_data_dup[i2] >= LnxqFirstDigit_Len)
                goto error;
            i3 = 2 * i2 - ((p_data_dup[1] & 0xFF) < 11 ? 1 : 0) - LnxqFirstDigit[(unsigned char) p_data_dup[i2]];
        }

        if (m != 0)
        {
            if (i7 >= 0)
            {
                m = i7 >= i6 ? 0 : 1;
            }
            else
            {
                m = ((i7 >= -5) || (i3 - i7 <= i6) || (i6 <= 6) ? 0 : 1) ^ 0x1;
            }
        }

        int i1 = 0;
        int i8 = 0;
        int i10 = 0;
        int i11 = 0;
        int i13 = 0;
        int i14 = 0;
        int i15 = 0;
        int i16 = 0;
        if (m != 0)
        {
            if (i7 >= 0)
            {
                i8 = i6 <= i7 + 1 ? i6 : i6 - 1;
            }
            else
            {
                i8 = i6 + i7;
            }

            if (i8 < i3)
            {
                char *p_data_dup_tmp = NULL;
                if (ora_lnxfpr(p_data_dup, ui_len, i8, &p_data_dup_tmp, &i_data_dup_len) != FR_SUCC)
                    goto error;
                frfree(p_data_dup);
                p_data_dup = p_data_dup_tmp;
                continue;
            }

            i18 = 0;
            if (i7 > 0)
            {
                if (i3 > i7 + 1)
                {
                    i4 = i6 - (i3 + 1);
                    i11 = 1;
                    i12 = 0;
                    i13 = (i7 & 0x1) > 0 ? 0 : 1;
                    i14 = ((i3 - i7) & 0x1) > 0 ? 0 : 1;
                    int i20 = 2147483647;
                    i15 = (i7 + 1) & (i20 - 1);
                    i16 = (i3 - (i7 + 1)) & (i20 - 1);
                    i1 = i4;
                }
                else
                {
                    i4 = i6 - (i7 + 1);
                    i10 = i7 + 1 - i3;
                    i11 = 0;
                    i13 = (i7 & 0x1) > 0 ? 0 : 1;
                    i14 = (i10 & 0x1) > 0 ? 1 : 0;
                    i15 = i3 - (i13 != 0 ? 1 : 0) - (i14 != 0 ? 1 : 0);
                    i16 = 0;

                    if (i10 != 0)
                    {
                        int n = i4 + (i_is_positive ? 1 : 0) + i3;
                        for (i17 = 0; i17 < i10; i17++)
                        {
                            p_char2[n + i17] = arr_char1[0];
                        }
                    }
                    i1 = i4;
                }
            }
            else
            {
                i4 = i6 - (i3 - i7);
                int i9 = -(i7 + 1);
                i11 = 0;
                i13 = (i9 & 0x1) > 0 ? 1 : 0;
                i14 = ((i9 + i3) & 0x1) > 0 ? 1 : 0;
                i15 = 0;
                i16 = i3 - (i13 != 0 ? 1 : 0) - (i14 != 0 ? 1 : 0);
                int n = i4;
                if (i_is_positive)
                {
                    p_char2[n] = arr_char1[11];
                    n++;
                    i_is_positive = 0;
                }

                p_char2[n] = (char) i5;
                n++;
                if (i9 != 0)
                {
                    for (i17 = 0; i17 < i9; i17++)
                        p_char2[n + i17] = arr_char1[0];
                    n += i9;
                }
                i1 = n;
            }

            if (i4 != 0)
            {
                for (i17 = 0; i17 < i4; i17++)
                {
                    p_char2[i17] = arr_char1[12];
                }
            }
        }
        else
        {
            i8 = i6 - ((i7 > 99) || (i7 < -99) ? 6 : 5);
            if (i8 < 2)
                goto error;

            if (i8 < i3)
            {
                char *p_data_dup_tmp = NULL;
                if (ora_lnxfpr(p_data_dup, ui_len, i8, &p_data_dup_tmp, &i_data_dup_len) != FR_SUCC)
                    goto error;
                frfree(p_data_dup);
                p_data_dup = p_data_dup_tmp;
                continue;
            }

            i18 = 0;
            if (i3 == 1)
            {
                i10 = i8 - 1;
                i11 = 1;
                i12 = (i7 & 0x1) > 0 ? 1 : 0;
                i13 = i12 ^ 0x1;
                i14 = 0;
                i15 = 0;
                i16 = 0;
            }
            else
            {
                i10 = i8 - i3;
                i11 = 1;
                i12 = (i7 & 0x1) > 0 ? 1 : 0;
                i13 = i12 ^ 0x1;
                i14 = i12 != ((i3 & 0x1) > 0 ? 1 : 0) ? 0 : 1;
                i15 = 0;
                i16 = i3 - (i13 != 0 ? 1 : 2) - (i14 != 0 ? 1 : 0);
            }

            int n = (i_is_positive ? 1 : 0) + 1 + i13;

            if (i10 != 0)
            {
                for (i17 = 0; i17 < i10; i17++)
                    p_char2[n + i17] = arr_char1[0];
                n += i10;
            }

            if (i7 < 0)
            {
                i7 = -i7;
                p_char2[n] = arr_char1[41];
                p_char2[n + 1] = arr_char1[11];
            }
            else
            {
                p_char2[n] = arr_char1[41];
                p_char2[n + 1] = arr_char1[10];
            }

            if ((arr_tmp[0] & 0xFF) < 0 || (arr_tmp[0] & 0xFF) >= LnxqComponents_P_Len)
                goto error;
            if (i7 > 99)
            {
                p_char2[n + 2] = arr_char1[1];
                arr_tmp[0] = (char) (i7 - 99);
                p_char2[n + 3] = arr_char1[(unsigned char) LnxqComponents_P[arr_tmp[0] & 0xFF][0]];
                p_char2[n + 4] = arr_char1[(unsigned char) LnxqComponents_P[arr_tmp[0] & 0xFF][1]];
            }
            else
            {
                arr_tmp[0] = (char) (i7 + 1);
                p_char2[n + 2] = arr_char1[(unsigned char) LnxqComponents_P[arr_tmp[0] & 0xFF][0]];
                p_char2[n + 3] = arr_char1[(unsigned char) LnxqComponents_P[arr_tmp[0] & 0xFF][1]];
            }

            i1 = 0;
        }

        int i = 1;
        int n = i1;

        if (i_is_positive)
        {
            p_char2[n] = arr_char1[11];
            n++;
        }

        if (i13 != 0)
        {
            if (p_data_dup[i] * 2 + 1 < 0 || p_data_dup[i] * 2 + 1 >= i_lnxqComponents_len)
                goto error;
            p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2 + 1]];
            n++;
            i++;
        }
        int j = 0;
        if (i15 != 0)
        {
            j = n + i15;
            while (n < j)
            {
                if (p_data_dup[i] * 2 < 0 || p_data_dup[i] * 2 >= i_lnxqComponents_len)
                    goto error;
                p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2]];
                n++;
                if (p_data_dup[i] * 2 + 1 < 0 || p_data_dup[i] * 2 + 1 >= i_lnxqComponents_len)
                    goto error;
                p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2 + 1]];
                n++;
                i++;
            }
        }

        if (i11 != 0)
        {
            if (i12 != 0)
            {
                if (p_data_dup[i] * 2 < 0 || p_data_dup[i] * 2 >= i_lnxqComponents_len)
                    goto error;
                p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2]];
                n++;
                p_char2[n] = (char) i5;
                n++;
                if (p_data_dup[i] * 2 + 1 < 0 || p_data_dup[i] * 2 + 1 >= i_lnxqComponents_len)
                    goto error;
                p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2 + 1]];
                n++;
                i++;
            }
            else
            {
                p_char2[n] = (char) i5;
                n++;
            }
        }

        if (i16 != 0)
        {
            j = n + i16;
            while (n < j)
            {
                if (p_data_dup[i] * 2 < 0 || p_data_dup[i] * 2 >= i_lnxqComponents_len)
                    goto error;
                p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2]];
                n++;
                if (p_data_dup[i] * 2 + 1 < 0 || p_data_dup[i] * 2 + 1 >= i_lnxqComponents_len)
                    goto error;
                p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2 + 1]];
                n++;
                i++;
            }
        }

        if (i14 != 0)
        {
            if (p_data_dup[i] * 2 < 0 || p_data_dup[i] * 2 >= i_lnxqComponents_len)
                goto error;
            p_char2[n] = arr_char1[(unsigned char) p_lnxqComponents[p_data_dup[i] * 2]];
            n++;
            i++;
        }
    }

    *pp_out = p_char2;

success:
    ora_free((void **) &p_data_dup);
    return FR_SUCC;
error:
    ora_free((void **) &p_char2);
    ora_free((void **) &p_data_dup);
    return FR_FAIL;
}
